package models;

import java.util.*;

import javax.persistence.*;

import play.db.jpa.Model;

@Entity
public class Idea extends Model {
	@ManyToOne
	public User author;
	public Date atDate;
	@Lob
	public String content;
	public String title;
	@OneToMany
	public List<Tag> tag;
	public int report;
	public int state;
	public ArrayList<Integer> rate;
	public ArrayList<Long> userRated;
    public int views;	
	@OneToMany
	public List<Comment>comments;
    @ManyToMany
	  public List<User> toBeNotified;
	public Date idealastactivity;
	
	@ManyToMany
	public List<Label>labels;

	 /**
	 * 
	 * @author Ahmed 
	 * @params the user who created the idea, and the content of the idea it self
	 */

	public Idea(User author,String title, String content) {
		toBeNotified = new ArrayList<User>();
		this.author = author;
		this.atDate = new Date();
		this.title=title;
		this.content = content;
		comments=new ArrayList<Comment>();
		userRated=new ArrayList<Long>();
		rate=new ArrayList<Integer>();
		state=1;
	}
	

	 public void addTag(Tag tag ) {
	 this.tag.add(tag);
	 }
	 
	 /**
		 * 
		 * @author emam
		 * @params flag  to be changed to true when the method called
		 */
		/*
		 * the method used for add 1 as a mark of reproting the idea
		 */ 
	 public void reportIdea(boolean flag) {
			if (flag == true)
				report=1;
			else report=0;
			}

	 
	 /**
	  * @author noreen baraka
	  * @return
	  * This method calculates the average rating for an idea to be viewed in the ShowTopic
	  */


	 public int averageRate()
	 {
	 	
	 	 int sum = 0;
	 	 int averageRate = 0;
	 	for(int i=0;i<rate.size();i++)
	 	{
	 		int ratings = rate.get(i);
	 		 sum=sum+ratings;
	 		 
	 	}
	 	if (rate.size() != 0) {
	 		averageRate=sum/rate.size();	 		
	 	} else {
	 		averageRate = 0;
	 	}
	      return averageRate;
	 	}


}